package com.ysd.lis.service.cri.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysRole;
import com.ysd.lis.entity.cri.CriRelease;
import com.ysd.lis.mapper.cri.CriReleaseMapper;
import com.ysd.lis.service.cri.CriReleaseService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 发布方式设置 服务实现类
 * </p>
 *
 * @author wh
 * @since 2025-02-11
 */
@Service
public class CriReleaseServiceImpl extends ServiceImpl<CriReleaseMapper, CriRelease> implements CriReleaseService {

    @Override
    public Result findReleaseist(String keywords) {
        MPJLambdaWrapper<CriRelease> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(CriRelease.class);
        lambdaQueryWrapper.selectAs(SysRole::getRoleName, CriRelease::getReleaseRoleNa);
        lambdaQueryWrapper.leftJoin(SysRole.class, p -> p.eq(CriRelease::getReleaseRole, SysRole::getCode).eq(SysRole::getDelFlag, 0));
        lambdaQueryWrapper.eq(CriRelease::getDelFlag, 0);
        if (StrUtil.isNotEmpty(keywords)) {
            lambdaQueryWrapper.and(wrapper ->
                    wrapper.like(CriRelease::getReleaseNo, keywords)
                            .or()
                            .like(CriRelease::getReleaseNa, keywords)
            );
        }
        List<CriRelease> sysPosts = this.baseMapper.selectList(lambdaQueryWrapper);
        return Result.succ(sysPosts);
    }

    @Override
    public Result saveOrUpdateRelease(CriRelease CriRelease) {
        boolean save = this.saveOrUpdate(CriRelease);
        if (save) {
            return Result.succ(1, "成功！", null);
        } else {
            return Result.fail("失败！");
        }
    }

    @Override
    public Result deleteReleaseList(List<CriRelease> sysPost) {
        List<String> ids = sysPost.stream().map(CriRelease::getId).collect(Collectors.toList());
        this.baseMapper.deleteBatchIds(ids);
        return Result.succ(1, "删除成功！", null);
    }
}
